﻿/* $RCSFile: WrapperData.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/WrapperData.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

using AddonNice.CRMWrapper.CRM3Server;
using AddonNice.CRMWrapper.CRM4Server;


namespace AddonNice.CRMWrapper
{
    /// <summary>
    /// Just a container for the real Crm structure
    /// </summary>
    public class InternalBaseData
    { 
        public object Content;
        public InternalBaseData(object o)
        {  Content= o;}
    }
    
    #region Metadata wrappers

    public enum InternalAttributeFields
    {
        //v3
        IsCustomField,
        AttributeOf,
        DefaultValue,
        Description,
        Name,
        DisplayName,
        RequiredLevel,
        DisplayMask,
        Type,
        ValidForCreate,
        ValidForRead,
        ValidForUpdate,
        //v4
        AggregateOf,
        AttributeType,
        CalculationOf,
        EntityLogicalName,
        LogicalName,
        MetadataId,
        SchemaName,
        YomiOf,
    }
    
    #region Attributes

    public enum DateTimeInternalFormat 
    {
        DateOnly,
        DateAndTime,
    }
    
    public enum StringInternalFormat 
    {
        Email,
        Text,
        TextArea,
        Url,
        TickerSymbol,
        PhoneticGuide,
    }

    public enum ImeInternalMode 
    {
        Auto,
        Inactive,
        Active,
        Disabled,
    }

    public enum IntegerInternalFormat 
    {
        None,
        Duration,
        TimeZone,
        Language,
        Locale,
    }
    
    #endregion Attributes
    
    #region relations
    

    public enum InternalRelationField
    {
        // v3
        Name,
        IsCustomRelationship,   //and v4
        ReferencedEntity,       // and v4 One2Many
        ReferencingEntity,      // and v4 One2Many
        ReferencedAttribute,    // and v4 One2Many
        ReferencingAttribute,   // and v4 One2Many
        // V4
        SchemaName,
        RelationshipType,
        IsValidForAdvancedFind,
        SecurityType,
        
        // V4 One2Many
        CascadeAssign,
        CascadeDelete,
        CascadeMerge,
        CascadeReparent,
        CascadeShare,
        CascadeUnshare,
        AssociatedMenuBehavior,
        AssociatedMenuGroup,
        AssociatedMenuOrder,
        AssociatedMenuLabel,
        
        // V4 Many2Many
        
        Entity1LogicalName,
        Entity2LogicalName,
        IntersectEntityName,
        Entity1IntersectAttribute,
        Entity2IntersectAttribute,
        Entity1AssociatedMenuBehavior,
        Entity1AssociatedMenuLabel,
        Entity2AssociatedMenuBehavior,
        Entity2AssociatedMenuLabel,
        Entity1AssociatedMenuGroup,
        Entity2AssociatedMenuGroup,
        Entity1AssociatedMenuOrder,
        Entity2AssociatedMenuOrder,
    }

    // V4
    public enum InternalEntityRelationshipType 
    {
        OneToMany, // v4
        ManyToMany,//v4
    }

    public enum InternalAssociatedMenuBehavior 
    {
        UseCollectionName,
        UseLabel,
        DoNotDisplay,
    }

    public enum InternalAssociatedMenuGroup 
    {
        Details,
        Sales,
        Service,
        Marketing,
    }

    public enum CascadeInternalType 
    {
        NoCascade,
        Cascade,
        Active,
        UserOwned,
        RemoveLink,
        Restrict,
    }

    #endregion relations
    
    public enum AttributeInternalType
    {        
        Boolean,
        Customer,
        DateTime,
        Decimal,
        Float,
        Integer,
        Internal,
        Lookup,
        Memo,
        Money,
        Owner,
        PartyList,
        Picklist,
        PrimaryKey,
        State,
        Status,
        String,
        UniqueIdentifier,
        Virtual,
        CalendarRules,
        
        EntityNameReference,
        WorkflowStateInfo,
        //Key, idem PrimaryKey ?
    }

    /// <summary>
    /// One to one correspondance with OwnershipTypes of v4
    /// </summary>
    [FlagsAttribute] 
    public enum OwnershipInternalTypes 
    {
        None                = 1,
        UserOwned           = 2,
        TeamOwned           = 4,
        BusinessOwned       = 8,
        OrgOwned            = 16,
        BusinessParented    = 32,
    }

    [FlagsAttribute] 
    public enum AttributeInternalRequiredLevel 
    {
        None,
        SystemRequired,
        Required,
        Recommended,
        ReadOnly,
    }
    
    [FlagsAttribute] 
    public enum EntityInternalItems 
    {
        EntityOnly = 1,
        IncludeAttributes = 2,
        IncludePrivileges = 4,
        IncludeRelationships = 8,
        All = 16,
    }
    
    #region v3 versions of flags beware the old v3 version has inverted values for IncludeRelationships and IncludePrivileges
   [FlagsAttribute] 
    public enum EntityInternalFlags 
    {
        EntityOnly = 1,
        IncludeAttributes = 2,
        IncludeRelationships = 4,
        IncludePrivileges = 8,
        All = 16,
    }
    
    #endregion v3 versions of flags beware the old v3 version has inverted values for IncludeRelationships and IncludePrivileges

    [System.FlagsAttribute()]
    public enum InternalDisplayMasks 
    {
        None = 1,
        PrimaryName = 2,
        ObjectTypeCode = 4,
        ValidForAdvancedFind = 8,
        ValidForForm = 16,
        ValidForGrid = 32,
        RequiredForForm = 64,
        RequiredForGrid = 128,
    }

    public class EntityInternalMetadata : InternalBaseData
    {
        public EntityInternalMetadata(object o) : base(o) {}
    }

    public class InternalMetadata : InternalBaseData 
    {
        public InternalMetadata(object o) : base(o) {}
    }

    public class InternalAttributeMetadata : InternalBaseData
    {
        public InternalAttributeMetadata(object o) : base(o) {}
    }
    
    public class InternalRelationMetadata: InternalBaseData
    {
        public InternalRelationMetadata(object o) : base(o) {}
    }


    #region Properties for dynEntity
    
    public class InternalProperty : InternalBaseData
    {
        public InternalProperty(object o) : base(o) {}
    }
    public class  StringInternalProperty : InternalProperty
    {
        public StringInternalProperty(object o) : base(o) {}
    }

    public class  CrmBooleanInternalProperty : InternalProperty
    {
        public CrmBooleanInternalProperty(object o) : base(o) {}
    }

    public class  CrmDateTimeInternalProperty : InternalProperty
    {
        public CrmDateTimeInternalProperty(object o) : base(o) {}
    }

    public class  CrmDecimalInternalProperty : InternalProperty
    {
        public CrmDecimalInternalProperty(object o) : base(o) {}
    }

    public class  CrmFloatInternalProperty : InternalProperty
    {
        public CrmFloatInternalProperty(object o) : base(o) {}
    }

    public class  CrmMoneyInternalProperty : InternalProperty
    {
        public CrmMoneyInternalProperty(object o) : base(o) {}
    }

    public class  CrmNumberInternalProperty : InternalProperty
    {
        public CrmNumberInternalProperty(object o) : base(o) {}
    }

    public class  CustomerInternalProperty : InternalProperty
    {
        public CustomerInternalProperty(object o) : base(o) {}
    }

    public class  DynamicEntityArrayInternalProperty : InternalProperty
    {
        public DynamicEntityArrayInternalProperty(object o) : base(o) {}
    }

    public class  EntityNameReferenceInternalProperty : InternalProperty
    {
        public EntityNameReferenceInternalProperty(object o) : base(o) {}
    }

    public class  KeyInternalProperty : InternalProperty
    {
        public KeyInternalProperty(object o) : base(o) {}
    }

    public class  LookupInternalProperty : InternalProperty
    {
        public LookupInternalProperty(object o) : base(o) {}
    }

    public class  CalendarRulesInternalProperty : InternalProperty
    {
        public CalendarRulesInternalProperty(object o) : base(o) {}
    }

    public class  OwnerInternalProperty : InternalProperty
    {
        public OwnerInternalProperty(object o) : base(o) {}
    }

    public class  PicklistInternalProperty : InternalProperty
    {
        public PicklistInternalProperty(object o) : base(o) {}
    }

    public class  StateInternalProperty : InternalProperty
    {
        public StateInternalProperty(object o) : base(o) {}
    }

    public class  StatusInternalProperty : InternalProperty
    {
        public StatusInternalProperty(object o) : base(o) {}
    }

    public class  UniqueIdentifierInternalProperty : InternalProperty
    {
        public UniqueIdentifierInternalProperty(object o) : base(o) {}
    }


    #endregion Properties for dynEntity

    #endregion Metadata wrappers

    #region Crm data wrappers

    [System.FlagsAttribute()]
    public enum InternalCrmAccessRights 
    {
        ReadAccess = 1,
        WriteAccess = 2,
        AppendAccess = 4,
        AppendToAccess = 8,
        CreateAccess = 16,
        DeleteAccess = 32,
        ShareAccess = 64,
        AssignAccess = 128,
    }

    
    public class InternalDynamicEntity : InternalBaseData
    {
        public InternalDynamicEntity(object o) : base(o) { }
    }
    
    public class InternalDynamicEntityArray : InternalBaseData
    {
        public InternalDynamicEntityArray(object o) : base(o) { }
    }

    public class InternalRolePrivilegeArray : InternalBaseData
    {
        public InternalRolePrivilegeArray(object o) : base(o) { }
    }

    // A developper avec chaque type de BusinessEntity
    public class InternalBusinessEntity : InternalBaseData
    {
        public InternalBusinessEntity(object o) : base(o) { }
    }

    public class InternalBusinessEntityArray : InternalBaseData
    {
        public InternalBusinessEntityArray(object o) : base(o) { }
    } 

    public class InternalBusinessEntityCollection : InternalBaseData
    {
        public InternalBusinessEntityCollection(object o) : base(o) { }
    } 
    
    public class InternalActivityParty : InternalBaseData
    {
        public InternalActivityParty(object o) : base(o) { }
    }

    public class InternalResponse : InternalBaseData
    {
        public InternalResponse(object o) : base(o) { }
    }
    
    public class InternalRetrieveMultipleResponse : InternalResponse 
    {
        public InternalRetrieveMultipleResponse(object o) : base(o) { }
    }

    public class InternalUpdateResponse : InternalResponse 
    {
        public InternalUpdateResponse(object o) : base(o) { }
    }


    public class InternalRequest : InternalBaseData
    {
        public InternalRequest(object o) : base(o) { }
    }

    public class InternalRetrieveMultipleRequest : InternalRequest 
    {
        public InternalRetrieveMultipleRequest(object o) : base(o) { }
    }    
    
    public class InternalUpdateRequest : InternalRequest 
    {
        public InternalUpdateRequest(object o) : base(o) { }
    }    

    public class InternalTargetCompound : InternalBaseData
    {
        public InternalTargetCompound(object o) : base(o) { }
    }
    
    public class InternalTargetCompoundDynamic : InternalTargetCompound 
    {
        public InternalTargetCompoundDynamic(object o) : base(o) { }
    }

    [TypeConverter(typeof(TypeConverterColumnSet))]
    public class InternalColumnSetBase
    {
		static public explicit operator CRM3Server.ColumnSetBase(InternalColumnSetBase val)
		{
			return (CRM3Server.ColumnSetBase) TypeDescriptor.GetConverter(typeof(InternalColumnSetBase)).ConvertTo(val, typeof(CRM3Server.ColumnSetBase));
		}
		static public explicit operator InternalColumnSetBase(CRM3Server.ColumnSetBase val)
		{
			return (InternalColumnSetBase) TypeDescriptor.GetConverter(typeof(InternalColumnSetBase)).ConvertTo(val, typeof(InternalColumnSetBase));
		}
		static public explicit operator CRM4Server.ColumnSetBase(InternalColumnSetBase val)
		{
			return (CRM4Server.ColumnSetBase) TypeDescriptor.GetConverter(typeof(InternalColumnSetBase)).ConvertTo(val, typeof(CRM4Server.ColumnSetBase));
		}
		static public explicit operator InternalColumnSetBase(CRM4Server.ColumnSetBase val)
		{
			return (InternalColumnSetBase) TypeDescriptor.GetConverter(typeof(InternalColumnSetBase)).ConvertTo(val, typeof(InternalColumnSetBase));
		}
    }
    
    [TypeConverter(typeof(TypeConverterColumnSet))]
    public class InternalAllColumns : InternalColumnSetBase
    {
		static public explicit operator CRM3Server.AllColumns(InternalAllColumns val)
		{
			return (CRM3Server.AllColumns) TypeDescriptor.GetConverter(typeof(InternalAllColumns)).ConvertTo(val, typeof(CRM3Server.AllColumns));
		}
		static public explicit operator InternalAllColumns(CRM3Server.AllColumns val)
		{
			return (InternalAllColumns) TypeDescriptor.GetConverter(typeof(InternalAllColumns)).ConvertTo(val, typeof(InternalAllColumns));
		}
		static public explicit operator CRM4Server.AllColumns(InternalAllColumns val)
		{
			return (CRM4Server.AllColumns) TypeDescriptor.GetConverter(typeof(InternalAllColumns)).ConvertTo(val, typeof(CRM4Server.AllColumns));
		}
		static public explicit operator InternalAllColumns(CRM4Server.AllColumns val)
		{
			return (InternalAllColumns) TypeDescriptor.GetConverter(typeof(InternalAllColumns)).ConvertTo(val, typeof(InternalAllColumns));
		}
    }

    [TypeConverter(typeof(TypeConverterColumnSet))]
    public class InternalColumnSet : InternalColumnSetBase
    {
        private string[] attributesField;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Attribute", IsNullable=false)]
        public string[] Attributes 
        {
            get 
            {
                return this.attributesField;
            }
            set 
            {
                this.attributesField = value;
            }
        }
    
    	/// <summary>
		/// Explicitly converts InternalColumnSet to ColumnSet value
		/// </summary>
		/// <returns></returns>
		static public explicit operator CRM3Server.ColumnSet(InternalColumnSet val)
		{
			return (CRM3Server.ColumnSet) TypeDescriptor.GetConverter(typeof(InternalColumnSet)).ConvertTo(val, typeof(CRM3Server.ColumnSet));
		}
		static public explicit operator CRM4Server.ColumnSet(InternalColumnSet val)
		{
			return (CRM4Server.ColumnSet) TypeDescriptor.GetConverter(typeof(InternalColumnSet)).ConvertTo(val, typeof(CRM4Server.ColumnSet));
		}

		/// <summary>
		/// Explicitly converts ColumSet to InternalColumnSet value
		/// </summary>
		/// <returns></returns>
		static public explicit operator InternalColumnSet(CRM3Server.ColumnSet val)
		{
			return (InternalColumnSet) TypeDescriptor.GetConverter(typeof(InternalColumnSet)).ConvertTo(val, typeof(InternalColumnSet));
		}
		static public explicit operator InternalColumnSet(CRM4Server.ColumnSet val)
		{
			return (InternalColumnSet) TypeDescriptor.GetConverter(typeof(InternalColumnSet)).ConvertTo(val, typeof(InternalColumnSet));
		}
    }

	/// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalColumnSet and ColumnSet values
	/// </summary>
	public class TypeConverterColumnSet : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet ConvertFrom BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (sourceType == typeof(InternalColumnSet)) 
			{
				return true;
			}
			if (sourceType == typeof(InternalAllColumns)) 
			{
				return true;
			}
			if (sourceType == typeof(InternalColumnSetBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.ColumnSet)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.AllColumns)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.ColumnSetBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.ColumnSet)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.AllColumns)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.ColumnSetBase)) 
			{
				return true;
			}
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet ConvertFrom END not found.",CRMQueryTraceSwitch.Sw.Info);
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet CanConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (sourceType == typeof(InternalColumnSet)) 
			{
				return true;
			}
			if (sourceType == typeof(InternalAllColumns)) 
			{
				return true;
			}
			if (sourceType == typeof(InternalColumnSetBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.ColumnSet)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.ColumnSetBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.AllColumns)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.ColumnSet)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.ColumnSetBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.AllColumns)) 
			{
				return true;
			}
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet CanConvertTo END not found.",CRMQueryTraceSwitch.Sw.Info);
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet ConvertFrom BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value is CRM3Server.ColumnSet) 
			{
				return ConvertFrom((CRM3Server.ColumnSet) value);
			}
			if (value is CRM3Server.AllColumns) 
			{
				return ConvertFrom((CRM3Server.AllColumns) value);
			}
			if (value is CRM4Server.ColumnSet) 
			{
				return ConvertFrom((CRM4Server.ColumnSet) value);
			}
			if (value is CRM4Server.AllColumns) 
			{
				return ConvertFrom((CRM4Server.AllColumns) value);
			}
			if (value is InternalColumnSet) 
			{
				return ConvertFrom((InternalColumnSet) value);
			}
			if (value is InternalAllColumns) 
			{
				return ConvertFrom((InternalAllColumns) value);
			}
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet ConvertFrom END not found.",CRMQueryTraceSwitch.Sw.Info);
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
            //CRM3Server
			if (destinationType == typeof(InternalColumnSet) &&
                ( value is CRM3Server.ColumnSet ) )
			{
				return ConvertFrom((CRM3Server.ColumnSet) value);
			}
			if (destinationType == typeof(InternalAllColumns) &&
                (value is CRM3Server.AllColumns)) 
			{
				return ConvertFrom((CRM3Server.AllColumns) value);
			}
			if (destinationType == typeof(InternalColumnSet) &&
                ( value is CRM4Server.ColumnSet ) )
			{
				return ConvertFrom((CRM4Server.ColumnSet) value);
			}
			if (destinationType == typeof(InternalAllColumns) &&
                (value is CRM4Server.AllColumns)) 
			{
				return ConvertFrom((CRM4Server.AllColumns) value);
			}
			//////
            if (destinationType == typeof(InternalColumnSetBase) &&
                ( value is CRM3Server.ColumnSet ) )
			{
				return ConvertFrom((CRM3Server.ColumnSet) value);
			}
			if (destinationType == typeof(InternalColumnSetBase) &&
                (value is CRM3Server.AllColumns)) 
			{
				return ConvertFrom((CRM3Server.AllColumns) value);
			}


			if (destinationType == typeof(CRM3Server.ColumnSet) &&
                ( value is InternalColumnSet )) 
			{
				return ConvertFrom((InternalColumnSet) value);
			}
			if (destinationType == typeof(CRM3Server.AllColumns) &&
                ( value is InternalAllColumns )) 
			{
				return ConvertFrom((InternalAllColumns) value);
			}
			// CRM4Server
            if (destinationType == typeof(InternalColumnSetBase) &&
                ( value is CRM4Server.ColumnSet ) )
			{
				return ConvertFrom((CRM4Server.ColumnSet) value);
			}
			if (destinationType == typeof(InternalColumnSetBase) &&
                (value is CRM4Server.AllColumns)) 
			{
				return ConvertFrom((CRM4Server.AllColumns) value);
			}
			if (destinationType == typeof(CRM4Server.ColumnSet) &&
                ( value is InternalColumnSet )) 
			{
				return ConvertFrom4((InternalColumnSet) value);
			}
			if (destinationType == typeof(CRM4Server.AllColumns) &&
                ( value is InternalAllColumns )) 
			{
				return ConvertFrom4((InternalAllColumns) value);
			}
			if (destinationType == typeof(CRM3Server.ColumnSetBase) &&
                ( value is InternalColumnSet )) 
			{
				return ConvertFrom((InternalColumnSet) value);
			}
			if (destinationType == typeof(CRM3Server.ColumnSetBase) &&
                ( value is InternalAllColumns )) 
			{
				return ConvertFrom((InternalAllColumns) value);
			}
			if (destinationType == typeof(CRM4Server.ColumnSetBase) &&
                ( value is InternalColumnSet )) 
			{
				return ConvertFrom4((InternalColumnSet) value);
			}
			if (destinationType == typeof(CRM4Server.ColumnSetBase) &&
                ( value is InternalAllColumns )) 
			{
				return ConvertFrom4((InternalAllColumns) value);
			}
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet ConvertTo END not found.",CRMQueryTraceSwitch.Sw.Info);
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.ColumnSet ConvertFrom(InternalColumnSet value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet CRM3Server.ColumnSet ConvertFrom BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            CRM3Server.ColumnSet cols      =   new CRM3Server.ColumnSet();
            cols.Attributes         =   (string[])value.Attributes.Clone();
            return cols;
		}

        public CRM3Server.AllColumns ConvertFrom(InternalAllColumns value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet CRM3Server.AllColumns ConvertFrom BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            return  new CRM3Server.AllColumns();
		}

        public InternalColumnSet ConvertFrom(CRM3Server.ColumnSet value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet InternalColumnSet ConvertFrom BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            InternalColumnSet cols      =   new InternalColumnSet();
            cols.Attributes         =   (string[])value.Attributes.Clone();
            return cols;
		}

        public InternalAllColumns ConvertFrom(CRM3Server.AllColumns value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet InternalAllColumns ConvertFrom BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            return new InternalAllColumns();
		}

    
    
        public CRM4Server.ColumnSet ConvertFrom4(InternalColumnSet value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet CRM4Server.ColumnSet ConvertFrom4 BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            CRM4Server.ColumnSet cols      =   new CRM4Server.ColumnSet();
            cols.Attributes         =   (string[])value.Attributes.Clone();
            return cols;
		}

        public CRM4Server.AllColumns ConvertFrom4(InternalAllColumns value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet CRM4Server.AllColumns ConvertFrom4 BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            return  new CRM4Server.AllColumns();
		}

        public InternalColumnSet ConvertFrom(CRM4Server.ColumnSet value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet InternalColumnSet ConvertFrom 4 BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            InternalColumnSet cols      =   new InternalColumnSet();
            cols.Attributes         =   (string[])value.Attributes.Clone();
            return cols;
		}

        public InternalAllColumns ConvertFrom(CRM4Server.AllColumns value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterColumnSet InternalAllColumns ConvertFrom 4 BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            return new InternalAllColumns();
		}
    }



    [TypeConverter(typeof(TypeConverterQueryExpression))]
    public abstract class InternalQueryBase
    {

        private string entityNameField;

        private InternalColumnSetBase columnSetField;

        /// <remarks/>
        public string EntityName
        {
            get
            {
                return this.entityNameField;
            }
            set
            {
                this.entityNameField = value;
            }
        }
        
        public InternalColumnSetBase ColumnSet 
        {
            get 
            {
                return this.columnSetField;
            }
            set 
            {
                this.columnSetField = value;
            }
        }
		static public explicit operator CRM3Server.QueryBase(InternalQueryBase val)
		{
			return (CRM3Server.QueryBase) TypeDescriptor.GetConverter(typeof(InternalQueryBase)).ConvertTo(val, typeof(CRM3Server.QueryBase));
		}

		static public explicit operator InternalQueryBase(CRM3Server.QueryBase val)
		{
			return (InternalQueryBase) TypeDescriptor.GetConverter(typeof(InternalQueryBase)).ConvertTo(val, typeof(InternalQueryBase));
		}
		static public explicit operator CRM4Server.QueryBase(InternalQueryBase val)
		{
			return (CRM4Server.QueryBase) TypeDescriptor.GetConverter(typeof(InternalQueryBase)).ConvertTo(val, typeof(CRM4Server.QueryBase));
		}

		static public explicit operator InternalQueryBase(CRM4Server.QueryBase val)
		{
			return (InternalQueryBase) TypeDescriptor.GetConverter(typeof(InternalQueryBase)).ConvertTo(val, typeof(InternalQueryBase));
		}
    }
    
    [TypeConverter(typeof(TypeConverterQueryExpression))]
    public class InternalQueryExpression : InternalQueryBase 
    {
        private bool distinctField;
        private InternalPagingInfo pageInfoField;
        
        private InternalLinkEntity[] linkEntitiesField;
        private InternalFilterExpression criteriaField;
        private InternalOrderExpression[] ordersField;
        
        public bool Distinct 
        {
            get 
            {
                return this.distinctField;
            }
            set 
            {
                this.distinctField = value;
            }
        }
        
        public InternalPagingInfo PageInfo 
        {
            get 
            {
                return this.pageInfoField;
            }
            set 
            {
                this.pageInfoField = value;
            }
        }
        
        public InternalLinkEntity[] LinkEntities 
        {
            get 
            {
                return this.linkEntitiesField;
            }
            set 
            {
                this.linkEntitiesField = value;
            }
        }
        
        public InternalFilterExpression Criteria 
        {
            get 
            {
                return this.criteriaField;
            }
            set 
            {
                this.criteriaField = value;
            }
        }
        
        public InternalOrderExpression[] Orders 
        {
            get 
            {
                return this.ordersField;
            }
            set 
            {
                this.ordersField = value;
            }
        }
		static public explicit operator CRM3Server.QueryExpression(InternalQueryExpression val)
		{
			return (CRM3Server.QueryExpression) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(CRM3Server.QueryExpression));
		}
		static public explicit operator CRM4Server.QueryExpression(InternalQueryExpression val)
		{
			return (CRM4Server.QueryExpression) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(CRM4Server.QueryExpression));
		}

		static public explicit operator CRM3Server.QueryBase(InternalQueryExpression val)
		{
			return (CRM3Server.QueryBase) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(CRM3Server.QueryBase));
		}
		static public explicit operator CRM4Server.QueryBase(InternalQueryExpression val)
		{
			return (CRM4Server.QueryBase) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(CRM4Server.QueryBase));
		}

        static public explicit operator InternalQueryExpression(CRM3Server.QueryExpression val)
		{
			return (InternalQueryExpression) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(InternalQueryExpression));
		}
		static public explicit operator InternalQueryExpression(CRM4Server.QueryExpression val)
		{
			return (InternalQueryExpression) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(InternalQueryExpression));
		}
        static public explicit operator InternalQueryExpression(CRM3Server.QueryBase val)
		{
			return (InternalQueryExpression) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(InternalQueryExpression));
		}
		static public explicit operator InternalQueryExpression(CRM4Server.QueryBase val)
		{
			return (InternalQueryExpression) TypeDescriptor.GetConverter(typeof(InternalQueryExpression)).ConvertTo(val, typeof(InternalQueryExpression));
		}
    }
    
    [TypeConverter(typeof(TypeConverterPagingInfo))]
    public class InternalPagingInfo 
    {
        private int pageNumberField;
        private int countField;
        private string pagingCookieField;
        
        public int PageNumber 
        {
            get 
            {
                return this.pageNumberField;
            }
            set 
            {
                this.pageNumberField = value;
            }
        }
        
        public int Count 
        {
            get 
            {
                return this.countField;
            }
            set 
            {
                this.countField = value;
            }
        }
        
        public string PagingCookie 
        {
            get 
            {
                return this.pagingCookieField;
            }
            set 
            {
                this.pagingCookieField = value;
            }
        }
		static public explicit operator CRM3Server.PagingInfo(InternalPagingInfo val)
		{
			return (CRM3Server.PagingInfo) TypeDescriptor.GetConverter(typeof(InternalPagingInfo)).ConvertTo(val, typeof(CRM3Server.PagingInfo));
		}

		static public explicit operator InternalPagingInfo(CRM3Server.PagingInfo val)
		{
			return (InternalPagingInfo) TypeDescriptor.GetConverter(typeof(InternalPagingInfo)).ConvertTo(val, typeof(InternalPagingInfo));
		}
		static public explicit operator CRM4Server.PagingInfo(InternalPagingInfo val)
		{
			return (CRM4Server.PagingInfo) TypeDescriptor.GetConverter(typeof(InternalPagingInfo)).ConvertTo(val, typeof(CRM4Server.PagingInfo));
		}

		static public explicit operator InternalPagingInfo(CRM4Server.PagingInfo val)
		{
			return (InternalPagingInfo) TypeDescriptor.GetConverter(typeof(InternalPagingInfo)).ConvertTo(val, typeof(InternalPagingInfo));
		}
    }
    
    [TypeConverter(typeof(TypeConverterLinkEntity))]
    public class InternalLinkEntity 
    {
        
        private string linkFromAttributeNameField;
        
        private string linkFromEntityNameField;
        
        private string linkToEntityNameField;
        
        private string linkToAttributeNameField;
        
        private InternalJoinOperator joinOperatorField;
        
        private InternalFilterExpression linkCriteriaField;
        
        private InternalLinkEntity[] linkEntitiesField;
        
        public string LinkFromAttributeName 
        {
            get 
            {
                return this.linkFromAttributeNameField;
            }
            set 
            {
                this.linkFromAttributeNameField = value;
            }
        }
        
        public string LinkFromEntityName 
        {
            get 
            {
                return this.linkFromEntityNameField;
            }
            set 
            {
                this.linkFromEntityNameField = value;
            }
        }
        
        public string LinkToEntityName 
        {
            get 
            {
                return this.linkToEntityNameField;
            }
            set 
            {
                this.linkToEntityNameField = value;
            }
        }
        
        public string LinkToAttributeName 
        {
            get 
            {
                return this.linkToAttributeNameField;
            }
            set 
            {
                this.linkToAttributeNameField = value;
            }
        }
        
        public InternalJoinOperator JoinOperator 
        {
            get {
                return this.joinOperatorField;
            }
            set 
            {
                this.joinOperatorField = value;
            }
        }
        
        public InternalFilterExpression LinkCriteria 
        {
            get 
            {
                return this.linkCriteriaField;
            }
            set 
            {
                this.linkCriteriaField = value;
            }
        }
        
        public InternalLinkEntity[] LinkEntities 
        {
            get 
            {
                return this.linkEntitiesField;
            }
            set 
            {
                this.linkEntitiesField = value;
            }
        }
		static public explicit operator CRM3Server.LinkEntity(InternalLinkEntity val)
		{
			return (CRM3Server.LinkEntity) TypeDescriptor.GetConverter(typeof(InternalLinkEntity)).ConvertTo(val, typeof(CRM3Server.LinkEntity));
		}
		static public explicit operator InternalLinkEntity(CRM3Server.LinkEntity val)
		{
			return (InternalLinkEntity) TypeDescriptor.GetConverter(typeof(InternalLinkEntity)).ConvertTo(val, typeof(InternalLinkEntity));
		}
		static public explicit operator CRM4Server.LinkEntity(InternalLinkEntity val)
		{
			return (CRM4Server.LinkEntity) TypeDescriptor.GetConverter(typeof(InternalLinkEntity)).ConvertTo(val, typeof(CRM4Server.LinkEntity));
		}
		static public explicit operator InternalLinkEntity(CRM4Server.LinkEntity val)
		{
			return (InternalLinkEntity) TypeDescriptor.GetConverter(typeof(InternalLinkEntity)).ConvertTo(val, typeof(InternalLinkEntity));
		}
    }
    
    public enum InternalJoinOperator 
    {
        Inner,
        LeftOuter,
        Natural,
    }

    [TypeConverter(typeof(TypeConverterFilterExpression))]
    public class InternalFilterExpression 
    {
        private InternalLogicalOperator filterOperatorField;
        private InternalConditionExpression[] conditionsField;
        private InternalFilterExpression[] filtersField;
        
        /// <remarks/>
        public InternalLogicalOperator FilterOperator 
        {
            get 
            {
                return this.filterOperatorField;
            }
            set 
            {
                this.filterOperatorField = value;
            }
        }
        
        public InternalConditionExpression[] Conditions 
        {
            get 
            {
                return this.conditionsField;
            }
            set 
            {
                this.conditionsField = value;
            }
        }
        
        public InternalFilterExpression[] Filters 
        {
            get 
            {
                return this.filtersField;
            }
            set 
            {
                this.filtersField = value;
            }
        }
		static public explicit operator CRM3Server.FilterExpression(InternalFilterExpression val)
		{
			return (CRM3Server.FilterExpression) TypeDescriptor.GetConverter(typeof(InternalFilterExpression)).ConvertTo(val, typeof(CRM3Server.FilterExpression));
		}
		static public explicit operator InternalFilterExpression(CRM3Server.FilterExpression val)
		{
			return (InternalFilterExpression) TypeDescriptor.GetConverter(typeof(InternalFilterExpression)).ConvertTo(val, typeof(InternalFilterExpression));
		}
		static public explicit operator CRM4Server.FilterExpression(InternalFilterExpression val)
		{
			return (CRM4Server.FilterExpression) TypeDescriptor.GetConverter(typeof(InternalFilterExpression)).ConvertTo(val, typeof(CRM4Server.FilterExpression));
		}
		static public explicit operator InternalFilterExpression(CRM4Server.FilterExpression val)
		{
			return (InternalFilterExpression) TypeDescriptor.GetConverter(typeof(InternalFilterExpression)).ConvertTo(val, typeof(InternalFilterExpression));
		}
    }
    
    public enum InternalLogicalOperator 
    {
        And,
        Or,
    }
    
    [TypeConverter(typeof(TypeConverterConditionExpression ))]
    public class InternalConditionExpression 
    {
        private string attributeNameField;
        private InternalConditionOperator operatorField;
        private object[] valuesField;
        
        public string AttributeName 
        {
            get 
            {
                return this.attributeNameField;
            }
            set 
            {
                this.attributeNameField = value;
            }
        }
        
        public InternalConditionOperator Operator 
        {
            get 
            {
                return this.operatorField;
            }
            set 
            {
                this.operatorField = value;
            }
        }
        
        public object[] Values 
        {
            get 
            {
                return this.valuesField;
            }
            set 
            {
                this.valuesField = value;
            }
        }
		static public explicit operator CRM3Server.ConditionExpression(InternalConditionExpression val)
		{
			return (CRM3Server.ConditionExpression) TypeDescriptor.GetConverter(typeof(InternalConditionExpression)).ConvertTo(val, typeof(CRM3Server.ConditionExpression));
		}
		static public explicit operator InternalConditionExpression(CRM3Server.ConditionExpression val)
		{
			return (InternalConditionExpression) TypeDescriptor.GetConverter(typeof(InternalConditionExpression)).ConvertTo(val, typeof(InternalConditionExpression));
		}
		static public explicit operator CRM4Server.ConditionExpression(InternalConditionExpression val)
		{
			return (CRM4Server.ConditionExpression) TypeDescriptor.GetConverter(typeof(InternalConditionExpression)).ConvertTo(val, typeof(CRM4Server.ConditionExpression));
		}
		static public explicit operator InternalConditionExpression(CRM4Server.ConditionExpression val)
		{
			return (InternalConditionExpression) TypeDescriptor.GetConverter(typeof(InternalConditionExpression)).ConvertTo(val, typeof(InternalConditionExpression));
		}
    }
    
    public enum InternalConditionOperator 
    {
        Equal,
        NotEqual,
        GreaterThan,
        LessThan,
        GreaterEqual,
        LessEqual,
        Like,
        NotLike,
        In,
        NotIn,
        Between,
        NotBetween,
        Null,
        NotNull,
        Yesterday,
        Today,
        Tomorrow,
        Last7Days,
        Next7Days,
        LastWeek,
        ThisWeek,
        NextWeek,
        LastMonth,
        ThisMonth,
        NextMonth,
        On,
        OnOrBefore,
        OnOrAfter,
        LastYear,
        ThisYear,
        NextYear,
        LastXHours,
        NextXHours,
        LastXDays,
        NextXDays,
        LastXWeeks,
        NextXWeeks,
        LastXMonths,
        NextXMonths,
        LastXYears,
        NextXYears,
        EqualUserId,
        NotEqualUserId,
        EqualBusinessId,
        NotEqualBusinessId,
    }
    
    [TypeConverter(typeof(TypeConverterOrderExpression))]
    public class InternalOrderExpression 
    {
        private string attributeNameField;
        private InternalOrderType orderTypeField;
        
        public string AttributeName 
        {
            get 
            {
                return this.attributeNameField;
            }
            set 
            {
                this.attributeNameField = value;
            }
        }
        
        public InternalOrderType OrderType 
        {
            get 
            {
                return this.orderTypeField;
            }
            set 
            {
                this.orderTypeField = value;
            }
        }
		static public explicit operator CRM3Server.OrderExpression(InternalOrderExpression val)
		{
			return (CRM3Server.OrderExpression) TypeDescriptor.GetConverter(typeof(InternalOrderExpression)).ConvertTo(val, typeof(CRM3Server.OrderExpression));
		}
		static public explicit operator InternalOrderExpression(CRM3Server.OrderExpression val)
		{
			return (InternalOrderExpression) TypeDescriptor.GetConverter(typeof(InternalOrderExpression)).ConvertTo(val, typeof(InternalOrderExpression));
		}
		static public explicit operator CRM4Server.OrderExpression(InternalOrderExpression val)
		{
			return (CRM4Server.OrderExpression) TypeDescriptor.GetConverter(typeof(InternalOrderExpression)).ConvertTo(val, typeof(CRM4Server.OrderExpression));
		}
		static public explicit operator InternalOrderExpression(CRM4Server.OrderExpression val)
		{
			return (InternalOrderExpression) TypeDescriptor.GetConverter(typeof(InternalOrderExpression)).ConvertTo(val, typeof(InternalOrderExpression));
		}
    }
    
    public enum InternalOrderType 
    {
        Ascending,
        Descending,
    }

	/// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalQueryExpression and Crm3QueryExpression values
	/// </summary>
	public class TypeConverterQueryExpression : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WrapperData TypeConverterQueryExpression CanConvertFrom BEG DestType: {0}.",sourceType),CRMQueryTraceSwitch.Sw.Info);
			if (sourceType == typeof(InternalQueryExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(InternalQueryBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.QueryExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.QueryBase)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.QueryExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.QueryBase)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type DestType) 
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WrapperData TypeConverterQueryExpression CanConvertTo BEG DestType: {0}.",DestType),CRMQueryTraceSwitch.Sw.Info);
			if (DestType == typeof(InternalQueryExpression)) 
			{
				return true;
			}
			if (DestType == typeof(InternalQueryBase)) 
			{
				return true;
			}
			if (DestType == typeof(CRM3Server.QueryExpression)) 
			{
				return true;
			}
			if (DestType == typeof(CRM3Server.QueryBase)) 
			{
				return true;
			}
			if (DestType == typeof(CRM4Server.QueryExpression)) 
			{
				return true;
			}
			if (DestType == typeof(CRM4Server.QueryBase)) 
			{
				return true;
			}
			return base.CanConvertTo(context, DestType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WrapperData TypeConverterQueryExpression ConvertFrom BEG value: {0}.",value),CRMQueryTraceSwitch.Sw.Info);
			if (value is CRM3Server.QueryExpression) 
			{
				return ConvertFrom((CRM3Server.QueryExpression) value);
			}
			if (value is CRM3Server.QueryBase) 
			{
				return ConvertFrom((CRM3Server.QueryBase) value);
			}

			if (value is CRM4Server.QueryExpression) 
			{
				return ConvertFrom((CRM4Server.QueryExpression) value);
			}

            if (value is CRM4Server.QueryBase) 
			{
				return ConvertFrom((CRM4Server.QueryBase) value);
			}
			if (value is InternalQueryExpression) 
			{
				return ConvertFrom((InternalQueryExpression) value);
			}
			if (value is InternalQueryBase) 
			{
				return ConvertFrom((InternalQueryBase) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
            if (destinationType == typeof(CRM3Server.QueryExpression) &&
                    value is InternalQueryExpression ) 
			{
				return ConvertFrom((InternalQueryExpression) value);
			}
            if (destinationType == typeof(CRM3Server.QueryBase) &&
                    value is InternalQueryExpression ) 
			{
				return ConvertFrom((InternalQueryExpression) value);
			}
            if (destinationType == typeof(CRM3Server.QueryBase) &&
                    value is InternalQueryBase ) 
			{
				return ConvertFrom((InternalQueryBase) value);
			}
			if (destinationType == typeof(InternalQueryExpression) &&
                value is CRM3Server.QueryExpression ) 
			{
				return ConvertFrom((CRM3Server.QueryExpression) value);
			}
			if (destinationType == typeof(InternalQueryBase) &&
                value is CRM3Server.QueryBase ) 
			{
				return ConvertFrom((CRM3Server.QueryBase) value);
			}
			if (destinationType == typeof(CRM4Server.QueryExpression) &&
                value is InternalQueryExpression ) 
			{
				return ConvertFrom4((InternalQueryExpression) value);
			}
			if (destinationType == typeof(CRM4Server.QueryBase) &&
                value is InternalQueryBase ) 
			{
				return ConvertFrom4((InternalQueryBase)value);
			}
			if (destinationType == typeof(CRM4Server.QueryBase) &&
                value is InternalQueryExpression ) 
			{
				return ConvertFrom4((InternalQueryExpression)value);
			}
			if (destinationType == typeof(InternalQueryExpression) &&
                value is CRM4Server.QueryExpression ) 
			{
				return ConvertFrom((CRM4Server.QueryExpression) value);
			}
			if (destinationType == typeof(InternalQueryBase) &&
                value is CRM4Server.QueryBase ) 
			{
				return ConvertFrom((CRM4Server.QueryBase) value);
			}

			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.QueryExpression ConvertFrom(InternalQueryExpression value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression CRM3Server ConvertFrom InternalQueryExpression BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            CRM3Server.QueryExpression quer        =   new CRM3Server.QueryExpression();
            quer.EntityName             =   value.EntityName;
            quer.Distinct               =   value.Distinct;
            quer.ColumnSet              =   (CRM3Server.ColumnSetBase)value.ColumnSet;
            quer.Criteria               =   (CRM3Server.FilterExpression)value.Criteria;
            if (value.LinkEntities != null)
            {
                quer.LinkEntities = new CRM3Server.LinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    quer.LinkEntities[i] = (CRM3Server.LinkEntity)value.LinkEntities[i];
            }
            if (value.Orders != null)
            {
                quer.Orders = new CRM3Server.OrderExpression[value.Orders.Length];
                for (int i = 0; i < value.Orders.Length; i++)
                    quer.Orders[i] = (CRM3Server.OrderExpression)value.Orders[i];
            }
            quer.PageInfo               =   (CRM3Server.PagingInfo)value.PageInfo;
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression CRM3Server ConvertFrom END.",CRMQueryTraceSwitch.Sw.Info);
            return quer;
		}

        public CRM3Server.QueryBase ConvertFrom(InternalQueryBase val)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression CRM3Server ConvertFrom InternalQueryBase BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (val == null)
				return null;
            if ( !(val is InternalQueryExpression ) )
                return null;
            InternalQueryExpression value       =   (InternalQueryExpression)val;
            CRM3Server.QueryExpression quer     =   new CRM3Server.QueryExpression();
            quer.EntityName                     =   value.EntityName;
            quer.Distinct                       =   value.Distinct;
            quer.ColumnSet                      =   (CRM3Server.ColumnSetBase)value.ColumnSet;
            quer.Criteria                       =   (CRM3Server.FilterExpression)value.Criteria;
            if (value.LinkEntities != null)
            {
                quer.LinkEntities               =   new CRM3Server.LinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    quer.LinkEntities[i]        =   (CRM3Server.LinkEntity)value.LinkEntities[i];
            }
            if (value.Orders != null)
            {
                quer.Orders                     =   new CRM3Server.OrderExpression[value.Orders.Length];
                for (int i = 0; i < value.Orders.Length; i++)
                    quer.Orders[i]              =   (CRM3Server.OrderExpression)value.Orders[i];
            }
            quer.PageInfo                       =   (CRM3Server.PagingInfo)value.PageInfo;
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression CRM3Server ConvertFrom END.",CRMQueryTraceSwitch.Sw.Info);
            return quer;
		}

        public CRM4Server.QueryExpression ConvertFrom4(InternalQueryExpression value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression CRM4Server ConvertFrom4 InternalQueryExpression BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            CRM4Server.QueryExpression quer        =   new CRM4Server.QueryExpression();
            quer.EntityName             =   value.EntityName;
            quer.Distinct               =   value.Distinct;
            quer.ColumnSet              =   (CRM4Server.ColumnSetBase)value.ColumnSet;
            quer.Criteria               =   (CRM4Server.FilterExpression)value.Criteria;
            if (value.LinkEntities != null)
            {
                quer.LinkEntities = new CRM4Server.LinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    quer.LinkEntities[i] = (CRM4Server.LinkEntity)value.LinkEntities[i];
            }

            if (value.Orders != null)
            {
                quer.Orders = new CRM4Server.OrderExpression[value.Orders.Length];
                for (int i = 0; i < value.Orders.Length; i++)
                    quer.Orders[i] = (CRM4Server.OrderExpression)value.Orders[i];
            }
            quer.PageInfo               =   (CRM4Server.PagingInfo)value.PageInfo;
            return quer;
		}

        public CRM4Server.QueryBase ConvertFrom4(InternalQueryBase val)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterQueryExpression CRM4Server ConvertFrom4 InternalQueryBase BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (val == null)
				return null;
            if ( !(val is InternalQueryExpression ) )
                return null;
            InternalQueryExpression value   =   (InternalQueryExpression)val;
            CRM4Server.QueryExpression quer =   new CRM4Server.QueryExpression();
            quer.EntityName                 =   value.EntityName;
            quer.Distinct                   =   value.Distinct;
            quer.ColumnSet                  =   (CRM4Server.ColumnSetBase)value.ColumnSet;
            quer.Criteria                   =   (CRM4Server.FilterExpression)value.Criteria;
            if (((InternalQueryExpression)value).LinkEntities != null)
            {
                quer.LinkEntities = new CRM4Server.LinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    quer.LinkEntities[i] = (CRM4Server.LinkEntity)value.LinkEntities[i];
            }

            if (value.Orders != null)
            {
                quer.Orders = new CRM4Server.OrderExpression[value.Orders.Length];
                for (int i = 0; i < value.Orders.Length; i++)
                    quer.Orders[i] = (CRM4Server.OrderExpression)value.Orders[i];
            }
            quer.PageInfo               =   (CRM4Server.PagingInfo)value.PageInfo;
            return quer;
		}
        
        
        public InternalQueryExpression ConvertFrom(CRM3Server.QueryExpression value)
		{
			if (value == null)
				return null;
            InternalQueryExpression quer      =   new InternalQueryExpression();
            quer.EntityName             =   value.EntityName;
            quer.Distinct               =   value.Distinct;
            quer.ColumnSet              =   (InternalColumnSetBase)value.ColumnSet;
            quer.Criteria               =   (InternalFilterExpression)value.Criteria;
            if (value.LinkEntities != null)
            {
                quer.LinkEntities = new InternalLinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    quer.LinkEntities[i] = (InternalLinkEntity)value.LinkEntities[i];
            }

            if (value.Orders != null)
            {
                quer.Orders = new InternalOrderExpression[value.Orders.Length];
                for (int i = 0; i < value.Orders.Length; i++)
                    quer.Orders[i] = (InternalOrderExpression)value.Orders[i];
            }
            quer.PageInfo               =   (InternalPagingInfo)value.PageInfo;
            return quer;
		}

        public InternalQueryExpression ConvertFrom(CRM4Server.QueryExpression value)
		{
			if (value == null)
				return null;
            InternalQueryExpression quer     =   new InternalQueryExpression();
            quer.EntityName             =   value.EntityName;
            quer.Distinct               =   value.Distinct;
            quer.ColumnSet              =   (InternalColumnSetBase)value.ColumnSet;
            quer.Criteria               =   (InternalFilterExpression)value.Criteria;
            if (value.LinkEntities != null)
            {
                quer.LinkEntities = new InternalLinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    quer.LinkEntities[i] = (InternalLinkEntity)value.LinkEntities[i];
            }

            if (value.Orders != null)
            {
                quer.Orders = new InternalOrderExpression[value.Orders.Length];
                for (int i = 0; i < value.Orders.Length; i++)
                    quer.Orders[i] = (InternalOrderExpression)value.Orders[i];
            }
            quer.PageInfo               =   (InternalPagingInfo)value.PageInfo;
            return quer;
		}
	}

	/// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalFilterExpression and Crm3FilterExpression values
	/// </summary>
	public class TypeConverterFilterExpression : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalFilterExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.FilterExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.FilterExpression)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalFilterExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.FilterExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.FilterExpression)) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
			if (value is CRM3Server.FilterExpression) 
			{
				return ConvertFrom((CRM3Server.FilterExpression) value);
			}
			if (value is CRM4Server.FilterExpression) 
			{
				return ConvertFrom((CRM4Server.FilterExpression) value);
			}
			if (value is InternalFilterExpression ) 
			{
				return ConvertFrom((InternalFilterExpression) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterFilterExpression ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
			if ( destinationType == typeof(CRM3Server.FilterExpression) &&
                value is InternalFilterExpression ) 
			{
				return ConvertFrom((InternalFilterExpression) value);
			}
			if (destinationType == typeof(CRM4Server.FilterExpression) &&
                value is InternalFilterExpression ) 
			{
				return ConvertFrom4((InternalFilterExpression) value);
			}
			if (destinationType == typeof(InternalFilterExpression) &&
                value is CRM3Server.FilterExpression) 
			{
				return ConvertFrom((CRM3Server.FilterExpression) value);
			}
			if (destinationType == typeof(InternalFilterExpression) &&
                value is CRM4Server.FilterExpression) 
			{
				return ConvertFrom((CRM4Server.FilterExpression) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.FilterExpression ConvertFrom(InternalFilterExpression value)
		{
			if (value == null)
				return null;
            CRM3Server.FilterExpression fi      =   new CRM3Server.FilterExpression();
            if (value.Conditions != null)
            {
                fi.Conditions = new CRM3Server.ConditionExpression[value.Conditions.Length];
                for (int i = 0; i < value.Conditions.Length; i++)
                    fi.Conditions[i] = (CRM3Server.ConditionExpression)value.Conditions[i];
            }
            fi.FilterOperator   =   (CRM3Server.LogicalOperator)(int)value.FilterOperator;
            if ( value.Filters != null)
            {
                fi.Filters = new CRM3Server.FilterExpression[value.Filters.Length];
                for (int i = 0; i < value.Filters.Length; i++)
                    fi.Filters[i] = (CRM3Server.FilterExpression)value.Filters[i];
            }
            return fi;
		}

        public InternalFilterExpression  ConvertFrom(CRM3Server.FilterExpression value)
		{
			if (value == null)
				return null;
            InternalFilterExpression fi      =   new InternalFilterExpression();
            if (value.Conditions != null)
            {
                fi.Conditions = new InternalConditionExpression[value.Conditions.Length];
                for (int i = 0; i < value.Conditions.Length; i++)
                    fi.Conditions[i] = (InternalConditionExpression)value.Conditions[i];
            }
            fi.FilterOperator   =   (InternalLogicalOperator)(int)value.FilterOperator;
            if (value.Filters != null)
            {
                fi.Filters = new InternalFilterExpression[value.Filters.Length];
                for (int i = 0; i < value.Filters.Length; i++)
                    fi.Filters[i] = (InternalFilterExpression)value.Filters[i];
            }
            return fi;
		}

        public CRM4Server.FilterExpression ConvertFrom4(InternalFilterExpression value)
		{
			if (value == null)
				return null;
            CRM4Server.FilterExpression fi      =   new CRM4Server.FilterExpression();
            if (value.Conditions != null)
            {
                fi.Conditions = new CRM4Server.ConditionExpression[value.Conditions.Length];
                for (int i = 0; i < value.Conditions.Length; i++)
                    fi.Conditions[i] = (CRM4Server.ConditionExpression)value.Conditions[i];
            }
            fi.FilterOperator   =   (CRM4Server.LogicalOperator)(int)value.FilterOperator;
            if (value.Filters != null)
            {
                fi.Filters = new CRM4Server.FilterExpression[value.Filters.Length];
                for (int i = 0; i < value.Filters.Length; i++)
                    fi.Filters[i] = (CRM4Server.FilterExpression)value.Filters[i];
            }
            return fi;
		}

        public InternalFilterExpression  ConvertFrom(CRM4Server.FilterExpression value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterFilterExpression ConvertFrom 4 BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            InternalFilterExpression fi      =   new InternalFilterExpression();
            if (value.Conditions != null)
            {
                if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WrapperData TypeConverterFilterExpression ConvertFrom 4 Conditions.Length: {0}.",
                        value.Conditions.Length),CRMQueryTraceSwitch.Sw.Info);
                fi.Conditions = new InternalConditionExpression[value.Conditions.Length];
                for (int i = 0; i < value.Conditions.Length; i++)
                    fi.Conditions[i] = (InternalConditionExpression)value.Conditions[i];
            }
            fi.FilterOperator   =   (InternalLogicalOperator)(int)value.FilterOperator;
            if (value.Filters != null)
            {
                fi.Filters = new InternalFilterExpression[value.Filters.Length];
                for (int i = 0; i < value.Filters.Length; i++)
                    fi.Filters[i] = (InternalFilterExpression)value.Filters[i];
            }
            return fi;
		}
	}

    /// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalOrderExpression and OrderExpression values
	/// </summary>
	public class TypeConverterOrderExpression : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalOrderExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.OrderExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.OrderExpression)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalOrderExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.OrderExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.OrderExpression)) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
			if (value is CRM3Server.OrderExpression) 
			{
				return ConvertFrom((CRM3Server.OrderExpression) value);
			}
			if (value is CRM4Server.OrderExpression) 
			{
				return ConvertFrom((CRM4Server.OrderExpression) value);
			}
			if (value is InternalOrderExpression) 
			{
				return ConvertFrom((InternalOrderExpression) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterOrderExpression ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
			if (destinationType == typeof(CRM3Server.OrderExpression)  &&
                value is InternalOrderExpression) 
			{
				return ConvertFrom((InternalOrderExpression) value);
			}
			if (destinationType == typeof(CRM4Server.OrderExpression) &&
                value is InternalOrderExpression ) 
			{
				return ConvertFrom4((InternalOrderExpression) value);
			}
			if (destinationType == typeof(InternalOrderExpression) &&
                value is CRM3Server.OrderExpression ) 
			{
				return ConvertFrom((CRM3Server.OrderExpression) value);
			}
			if (destinationType == typeof(InternalOrderExpression) &&
                value is CRM4Server.OrderExpression ) 
			{
				return ConvertFrom((CRM4Server.OrderExpression) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.OrderExpression ConvertFrom(InternalOrderExpression value)
		{
			if (value == null)
				return null;
            CRM3Server.OrderExpression oe      =   new CRM3Server.OrderExpression();
            oe.AttributeName    =   value.AttributeName;
            oe.OrderType        =   (CRM3Server.OrderType)(int)value.OrderType;
            return oe;
		}

        public CRM4Server.OrderExpression ConvertFrom4(InternalOrderExpression value)
		{
			if (value == null)
				return null;
            CRM4Server.OrderExpression oe      =   new CRM4Server.OrderExpression();
            oe.AttributeName    =   value.AttributeName;
            oe.OrderType        =   (CRM4Server.OrderType)(int)value.OrderType;
            return oe;
		}

        public InternalOrderExpression  ConvertFrom(CRM3Server.OrderExpression value)
		{
			if (value == null)
				return null;
            InternalOrderExpression oe      =   new InternalOrderExpression();
            oe.AttributeName    =   value.AttributeName;
            oe.OrderType        =   (InternalOrderType)(int)value.OrderType;
            return oe;
		}
        public InternalOrderExpression  ConvertFrom(CRM4Server.OrderExpression value)
		{
			if (value == null)
				return null;
            InternalOrderExpression oe      =   new InternalOrderExpression();
            oe.AttributeName    =   value.AttributeName;
            oe.OrderType        =   (InternalOrderType)(int)value.OrderType;
            return oe;
		}
	}
    /// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalConditionExpression and ConditionExpression values
	/// </summary>
	public class TypeConverterConditionExpression : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalConditionExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.ConditionExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.ConditionExpression)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalConditionExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.ConditionExpression)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.ConditionExpression)) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
			if (value is CRM3Server.ConditionExpression) 
			{
				return ConvertFrom((CRM3Server.ConditionExpression) value);
			}
			if (value is CRM4Server.ConditionExpression) 
			{
				return ConvertFrom((CRM4Server.ConditionExpression) value);
			}
			if (value is InternalConditionExpression) 
			{
				return ConvertFrom((InternalConditionExpression) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterConditionExpression ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
			if ( destinationType == typeof(CRM3Server.ConditionExpression) &&
                value is InternalConditionExpression ) 
			{
				return ConvertFrom((InternalConditionExpression) value);
			}
			if ( destinationType == typeof(CRM4Server.ConditionExpression) &&
                value is InternalConditionExpression )  
			{
				return ConvertFrom4((InternalConditionExpression) value);
			}
			if (destinationType == typeof(InternalConditionExpression) &&
                value is CRM3Server.ConditionExpression ) 
			{
				return ConvertFrom((CRM3Server.ConditionExpression) value);
			}
			if (destinationType == typeof(InternalConditionExpression) &&
                value is CRM4Server.ConditionExpression ) 
			{
				return ConvertFrom((CRM4Server.ConditionExpression) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.ConditionExpression ConvertFrom(InternalConditionExpression value)
		{
			if (value == null)
				return null;
            CRM3Server.ConditionExpression ce      =   new CRM3Server.ConditionExpression();
            ce.AttributeName    =   value.AttributeName;
            ce.Operator         =   (CRM3Server.ConditionOperator)(int)value.Operator;
            ce.Values           =   value.Values;
            return ce;
		}

        public CRM4Server.ConditionExpression ConvertFrom4(InternalConditionExpression value)
		{
			if (value == null)
				return null;
            CRM4Server.ConditionExpression ce      =   new CRM4Server.ConditionExpression();
            ce.AttributeName    =   value.AttributeName;
            ce.Operator         =   (CRM4Server.ConditionOperator)(int)value.Operator;
            ce.Values           =   value.Values;
            return ce;
		}

        public InternalConditionExpression ConvertFrom(CRM3Server.ConditionExpression value)
		{
			if (value == null)
				return null;
            InternalConditionExpression ce      =   new InternalConditionExpression();
            ce.AttributeName    =   value.AttributeName;
            ce.Operator         =   (InternalConditionOperator)(int)value.Operator;
            ce.Values           =   value.Values;
            return ce;
		}
        public InternalConditionExpression  ConvertFrom(CRM4Server.ConditionExpression value)
		{
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterConditionExpression ConvertFrom 4 BEG.",CRMQueryTraceSwitch.Sw.Info);
			if (value == null)
				return null;
            InternalConditionExpression ce      =   new InternalConditionExpression();
            ce.AttributeName    =   value.AttributeName;
            ce.Operator         =   (InternalConditionOperator)(int)value.Operator;
            ce.Values           =   value.Values;
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WrapperData TypeConverterConditionExpression ConvertFrom 4 AttributeName: {0}, Operator: {1}, Values: {2}.",
                    ce.AttributeName,ce.Operator,ce.Values),CRMQueryTraceSwitch.Sw.Info);
            return ce;
		}
    }

    /// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalPagingInfo and PagingInfo values
	/// </summary>
	public class TypeConverterPagingInfo : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalPagingInfo)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.PagingInfo)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.PagingInfo)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalPagingInfo)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.PagingInfo)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.PagingInfo)) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
			if (value is CRM3Server.PagingInfo) 
			{
				return ConvertFrom((CRM3Server.PagingInfo) value);
			}
			if (value is CRM4Server.PagingInfo) 
			{
				return ConvertFrom((CRM4Server.PagingInfo) value);
			}
			if (value is InternalPagingInfo) 
			{
				return ConvertFrom((InternalPagingInfo) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterPagingInfo ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
			if (destinationType == typeof(CRM3Server.PagingInfo)) 
			{
				return ConvertFrom((InternalPagingInfo) value);
			}
			if (destinationType == typeof(CRM4Server.PagingInfo)) 
			{
				return ConvertFrom4((InternalPagingInfo) value);
			}
			if (destinationType == typeof(InternalPagingInfo) &&
                value is CRM3Server.PagingInfo ) 
			{
				return ConvertFrom((CRM3Server.PagingInfo) value);
			}
			if (destinationType == typeof(InternalPagingInfo) &&
                value is CRM4Server.PagingInfo ) 
			{
				return ConvertFrom((CRM4Server.PagingInfo) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.PagingInfo ConvertFrom(InternalPagingInfo value)
		{
			if (value == null)
				return null;
            CRM3Server.PagingInfo pi      =   new CRM3Server.PagingInfo();
            pi.Count                    =   value.Count;
            pi.PageNumber               =   value.PageNumber;
            pi.PagingCookie             =   value.PagingCookie;
            return pi;
		}

        public CRM4Server.PagingInfo ConvertFrom4(InternalPagingInfo value)
		{
			if (value == null)
				return null;
            CRM4Server.PagingInfo pi    =   new CRM4Server.PagingInfo();
            pi.Count                    =   value.Count;
            pi.PageNumber               =   value.PageNumber;
            pi.PagingCookie             =   value.PagingCookie;
            return pi;
		}

        public InternalPagingInfo  ConvertFrom(CRM3Server.PagingInfo value)
		{
			if (value == null)
				return null;
            InternalPagingInfo pi            =   new InternalPagingInfo();
            pi.Count                    =   value.Count;
            pi.PageNumber               =   value.PageNumber;
            pi.PagingCookie             =   value.PagingCookie;
            return pi;
		}
        public InternalPagingInfo  ConvertFrom(CRM4Server.PagingInfo value)
		{
			if (value == null)
				return null;
            InternalPagingInfo pi            =   new InternalPagingInfo();
            pi.Count                    =   value.Count;
            pi.PageNumber               =   value.PageNumber;
            pi.PagingCookie             =   value.PagingCookie;
            return pi;
		}
	}

    /// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalLinkEntity and LinkEntity values
	/// </summary>
	public class TypeConverterLinkEntity : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalLinkEntity)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.LinkEntity)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.LinkEntity)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalLinkEntity)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.LinkEntity)) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.LinkEntity)) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
			if (value is CRM3Server.LinkEntity) 
			{
				return ConvertFrom((CRM3Server.LinkEntity) value);
			}
			if (value is CRM4Server.LinkEntity) 
			{
				return ConvertFrom((CRM4Server.LinkEntity) value);
			}
			if (value is InternalLinkEntity) 
			{
				return ConvertFrom((InternalLinkEntity) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterLinkEntity ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
			if (destinationType == typeof(CRM3Server.LinkEntity)) 
			{
				return ConvertFrom((InternalLinkEntity) value);
			}
			if (destinationType == typeof(CRM4Server.LinkEntity)) 
			{
				return ConvertFrom4((InternalLinkEntity) value);
			}
			if (destinationType == typeof(InternalLinkEntity) &&
                value is CRM3Server.LinkEntity ) 
			{
				return ConvertFrom((CRM3Server.LinkEntity ) value);
			}
			if (destinationType == typeof(InternalLinkEntity) &&
                value is CRM4Server.LinkEntity ) 
			{
				return ConvertFrom((CRM4Server.LinkEntity ) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.LinkEntity ConvertFrom(InternalLinkEntity value)
		{
			if (value == null)
				return null;
            CRM3Server.LinkEntity le        =   new CRM3Server.LinkEntity();
            le.JoinOperator                 =   (CRM3Server.JoinOperator)(int)value.JoinOperator;
            le.LinkCriteria                 =   (CRM3Server.FilterExpression)value.LinkCriteria;
            if (value.LinkEntities != null)
            {
                le.LinkEntities = new CRM3Server.LinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    le.LinkEntities[i] = (CRM3Server.LinkEntity)value.LinkEntities[i];
            }
            le.LinkFromAttributeName        =   value.LinkFromAttributeName;
            le.LinkFromEntityName           =   value.LinkFromEntityName;
            le.LinkToAttributeName          =   value.LinkToAttributeName;
            le.LinkToEntityName             =   value.LinkToEntityName;
            return le;
		}

        public CRM4Server.LinkEntity ConvertFrom4(InternalLinkEntity value)
		{
			if (value == null)
				return null;
            CRM4Server.LinkEntity le        =   new CRM4Server.LinkEntity();
            le.JoinOperator                 =   (CRM4Server.JoinOperator)(int)value.JoinOperator;
            le.LinkCriteria                 =   (CRM4Server.FilterExpression)value.LinkCriteria;
            if (value.LinkEntities != null)
            {
                le.LinkEntities = new CRM4Server.LinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    le.LinkEntities[i] = (CRM4Server.LinkEntity)value.LinkEntities[i];
            }
            le.LinkFromAttributeName        =   value.LinkFromAttributeName;
            le.LinkFromEntityName           =   value.LinkFromEntityName;
            le.LinkToAttributeName          =   value.LinkToAttributeName;
            le.LinkToEntityName             =   value.LinkToEntityName;
            return le;
		}

        public InternalLinkEntity  ConvertFrom(CRM3Server.LinkEntity value)
		{
			if (value == null)
				return null;
            InternalLinkEntity le                =   new InternalLinkEntity();
            le.JoinOperator                 =   (InternalJoinOperator)(int)value.JoinOperator;
            le.LinkCriteria                 =   (InternalFilterExpression)value.LinkCriteria;
            if (value.LinkEntities != null)
            {
                le.LinkEntities = new InternalLinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    le.LinkEntities[i] = (InternalLinkEntity)value.LinkEntities[i];
            }
            le.LinkFromAttributeName        =   value.LinkFromAttributeName;
            le.LinkFromEntityName           =   value.LinkFromEntityName;
            le.LinkToAttributeName          =   value.LinkToAttributeName;
            le.LinkToEntityName             =   value.LinkToEntityName;
            return le;
		}

        public InternalLinkEntity  ConvertFrom(CRM4Server.LinkEntity value)
		{
			if (value == null)
				return null;
            InternalLinkEntity le                =   new InternalLinkEntity();
            le.JoinOperator                 = (InternalJoinOperator)(int)value.JoinOperator;
            le.LinkCriteria                 =   (InternalFilterExpression)value.LinkCriteria;
            if (value.LinkEntities != null)
            {
                le.LinkEntities = new InternalLinkEntity[value.LinkEntities.Length];
                for (int i = 0; i < value.LinkEntities.Length; i++)
                    le.LinkEntities[i] = (InternalLinkEntity)value.LinkEntities[i];
            }
            le.LinkFromAttributeName        =   value.LinkFromAttributeName;
            le.LinkFromEntityName           =   value.LinkFromEntityName;
            le.LinkToAttributeName          =   value.LinkToAttributeName;
            le.LinkToEntityName             =   value.LinkToEntityName;
            return le;
		}
	}
    /// <summary>
	/// TypeConverterNameValue converter can convert From/To 
	/// InternalLinkEntity and LinkEntity values
	/// </summary>
	public class TypeConverterLinkEntityArray : TypeConverter
	{
		// Overrides the CanConvertFrom method of TypeConverter.
		// The ITypeDescriptorContext interface provides the context for the
		// conversion. Typically this interface is used at design time to 
		// provide information about the design-time container.
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalLinkEntity[])) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.LinkEntity[])) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.LinkEntity[])) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		public override  bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(InternalLinkEntity[])) 
			{
				return true;
			}
			if (sourceType == typeof(CRM3Server.LinkEntity[])) 
			{
				return true;
			}
			if (sourceType == typeof(CRM4Server.LinkEntity[])) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

        
   		public override  object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value) 
		{ 
			if (value is CRM3Server.LinkEntity[]) 
			{
				return ConvertFrom((CRM3Server.LinkEntity[]) value);
			}
			if (value is CRM4Server.LinkEntity[]) 
			{
				return ConvertFrom((CRM4Server.LinkEntity[]) value);
			}
			if (value is InternalLinkEntity[]) 
			{
				return ConvertFrom((InternalLinkEntity[]) value);
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]WrapperData TypeConverterLinkEntityArray ConvertTo BEG.",CRMQueryTraceSwitch.Sw.Info);
            if ( value == null )
                return null;
			if (destinationType == typeof(CRM3Server.LinkEntity[])) 
			{
				return ConvertFrom((CRM3Server.LinkEntity[]) value);
			}
			if (destinationType == typeof(CRM4Server.LinkEntity[])) 
			{
				return ConvertFrom((CRM4Server.LinkEntity[]) value);
			}
			if (destinationType == typeof(InternalLinkEntity[])) 
			{
				return ConvertFrom((InternalLinkEntity[]) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

        public CRM3Server.LinkEntity[] ConvertFrom(InternalLinkEntity[] value)
		{
			if (value == null)
				return null;
            CRM3Server.LinkEntity[] le      =   new CRM3Server.LinkEntity[value.Length];
            for ( int i=0; i < value.Length; i++)
                le[i]   =   (CRM3Server.LinkEntity)value[i];
            return le;
		}

        public InternalLinkEntity[]  ConvertFrom(CRM3Server.LinkEntity[] value)
		{
			if (value == null)
				return null;
            InternalLinkEntity[] le      =   new InternalLinkEntity[value.Length];
            for ( int i=0; i < value.Length; i++)
                le[i]   =   (InternalLinkEntity)value[i];
            return le;
		}
        public InternalLinkEntity[]  ConvertFrom(CRM4Server.LinkEntity[] value)
		{
			if (value == null)
				return null;
            InternalLinkEntity[] le      =   new InternalLinkEntity[value.Length];
            for ( int i=0; i < value.Length; i++)
                le[i]   =   (InternalLinkEntity)value[i];
            return le;
		}
	}
    #endregion Crm data wrappers
}
